<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>下载页面</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">


</head>

<body>
<style>
    .el-row {
        margin: 20px;
    }
</style>
<div id="app">
    <el-row>
        <el-upload class="upload-demo" action="http://westudy.life:12345/file/upload" :on-success="handleUpload"
                   multiple :limit="100">
            <el-button @click="toLearn()" size="small" type="success">跳转学习页面</el-button>
            <el-button size="small" type="primary">点击上传文件</el-button>
        </el-upload>
    </el-row>
    <el-row>
        <el-table :data="tableData" border stripe style="width: 801px">
            <el-table-column prop="uploadTime" label="上传时间" width="300">
            </el-table-column>
            <el-table-column prop="fileName" label="文件名" width="300">
            </el-table-column>
            <el-table-column label="下载" width="100">
                <template slot-scope="scope">
                    <el-button @click="confirmDownload(scope.row)" type="text" size="small">下载文件</el-button>
                </template>
            </el-table-column>
            <el-table-column label="删除" width="100">
                <template slot-scope="scope">
                    <el-button @click="confirmDelete(scope.row)" type="text" size="small">删除文件</el-button>
                </template>
            </el-table-column>

        </el-table>
    </el-row>

</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>

    // var baseUrl = "http://localhost:12345";
    var baseUrl = "http://westudy.life:12345";

    var app = new Vue({
        el: '#app',
        data() {
            var self = this;
            return {
                tableData: [],
            }
        },
        methods: {

            toLearn() {
                var get = "http://westudy.life:12345/learn.html";
                window.open(get, "_blank");
            },

            handleUpload(file, fileList) {
                location.reload();
            },


            confirmDownload(row) {
                this.$confirm('是否下载' + row.fileName, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$options.methods.download(row.id);
                this.$message({
                    type: 'success',
                    message: '下载成功!'
                });
            }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消下载'
                    });
            });
            },
            download(id) {
                var get = baseUrl + "/file/download?id=" + id;
                window.open(get, "_blank");
            },


            confirmDelete(row) {
                this.$confirm('此操作将永久删除' + row.fileName + ', 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$options.methods.delete(row.id);

            }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
            });
            },
            delete(id) {
                var http = new XMLHttpRequest();
                var get = baseUrl + "/file/delete?id=" + id;
                http.open("GET", get);
                http.send();
                location.reload();
            },
        }

    });

    function setScanData() {

        var http = new XMLHttpRequest();
        var get = baseUrl + "/file/scan";
        http.open("GET", get);
        http.send();
        var list = [];
        var self = this;
        http.onreadystatechange = async function () {
            var res = http.responseText;
            if (res != null && res.length != 0) {
                list = JSON.parse(res);
                app.$data.tableData = Array.from(list);
            }
        };

    }
    setScanData();

</script>

</html>